package com.gitee.qdbp.jdbc.plugins.impl;

import java.util.HashMap;
import java.util.Map;
import com.gitee.qdbp.jdbc.model.DbVersion;
import com.gitee.qdbp.jdbc.model.SimpleFieldColumn;
import com.gitee.qdbp.jdbc.plugins.DbPluginHelper;
import com.gitee.qdbp.jdbc.plugins.SqlDialect;
import com.gitee.qdbp.tools.property.PropertyContainer;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 根据配置获取字符长度的字段值校验处理类
 *
 * @author zhaohuihua
 * @version 20210801
 */
public class ConfigableColumnValueValidator extends SimpleColumnValueValidator implements DbPluginHelper.Aware {

    private DbPluginHelper plugins;
    private final Map<DbVersion, Map<Character, Integer>> cache = new HashMap<>();

    @Override
    protected Map<Character, Integer> getCharLengthMaps(String tableName, SimpleFieldColumn column,
            SqlDialect dialect) {
        DbVersion dbVersion = dialect.dbVersion();
        if (cache.containsKey(dbVersion)) {
            return cache.get(dbVersion);
        }
        PropertyContainer config = plugins.getSqlConfig();
        String value = config.getStringUseSuffix("charlength", dbVersion.toVersionString());
        Map<Character, Integer> charLengthMaps;
        if (VerifyTools.isBlank(value)) {
            charLengthMaps = defaultCharLengthMaps;
        } else {
            charLengthMaps = parseCharLengthMaps(value);
        }
        cache.put(dbVersion, charLengthMaps);
        return charLengthMaps;
    }

    @Override
    public void setPlugins(DbPluginHelper plugins) {
        this.plugins = plugins;
    }
}
